﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;
using System.Data;
using System.Data.Entity;
using Microsoft.Practices.Unity;
using Vit.Common;
using Vit.Common.Providers;
using Vit.Web;
using Vit.Web.Modules;
using Vit.Web.Models;
using Vit.Web.Providers;
using Vit.Web.Providers.DataSource;
using Vit.Web.Extensions;
using Vit.Web.Helpers;
using Vit.Web.Providers.Captcha;
using Vit.AdminModule.Areas.Admin.Providers;

namespace Vit.AdminModule.Areas.Admin
{
    public class AdminAreaRegistration  : Vit.Web.Modules.AreaModuleBase
    {
        public AdminAreaRegistration()
        {
            Version = new Version(1, 0, 0);
        }
        
        public override string AreaName
        {
            get
            {
                return "Admin";
            }
        }

        public override void RegisterArea(AreaRegistrationContext context)
        {
            context.MapRoute(
                "Admin_default",
                "Admin/{controller}/{action}/{id}",
                new { action = "Index", id = UrlParameter.Optional }
            );
        }

        public override void OnApplicationStart(HttpApplication application, IUnityContainer unity)
        {
            PortalHelper.RegisterWidgets(typeof(Vit.Web.Mvc.Controllers.ControllerBase).Assembly);
            Database.SetInitializer<WebProvidersDb>(new DropCreateDatabaseIfModelChanges<WebProvidersDb>());
            //Database.SetInitializer<WebProvidersDb>(new Vit.Web.Data.DoNothingDatabaseInitializer<WebProvidersDb>());
            ICacheProvider dataCache = unity.Resolve<ICacheProvider>();
            unity
                .RegisterInstance(new WebProvidersDb(), new HttpContextRequestLifetimeManager())
                // register providers
                .RegisterType<IRandomTextGenerator, RNGRandomTextGenerator>(new ContainerControlledLifetimeManager())
                .RegisterType<ICaptchaGenerator, DefaultCaptchaGenerator>(new ContainerControlledLifetimeManager())
                .RegisterType<IFriendlyUrlProvider, SqlFriendlyUrlProvider>(new HttpContextRequestLifetimeManager())
                .RegisterType<ILogProvider, SqlLogProvider>(new HttpContextRequestLifetimeManager())
                .RegisterType<IMembershipProvider, SqlMembershipProvider>(new HttpContextRequestLifetimeManager())
                .RegisterType<INavigationProvider, SqlNavigationProvider>(new HttpContextRequestLifetimeManager())
                .RegisterType<IPortalProvider, SqlPortalProvider>(new HttpContextRequestLifetimeManager())
                .RegisterType<IModuleProvider, SqlModuleProvider>(new HttpContextRequestLifetimeManager())
                .RegisterType<ICategoryProvider, SqlCategoryProvider>(new HttpContextRequestLifetimeManager())
                .RegisterType<ITagProvider, SqlTagProvider>(new HttpContextRequestLifetimeManager())
                .RegisterType<ITrackProvider, SqlTrackProvider>(new HttpContextRequestLifetimeManager())
                .RegisterType<IRelationProvider, SqlRelationProvider>(new HttpContextRequestLifetimeManager())
                // register enum datasources
                .RegisterInstance<IDataSourceProvider>("DataSource:DataStatus", new EnumDataSourceProvider(typeof(DataStatus), dataCache), new ContainerControlledLifetimeManager())
                .RegisterInstance<IDataSourceProvider>("DataSource:LogLevel", new EnumDataSourceProvider(typeof(LogLevel), dataCache), new ContainerControlledLifetimeManager())
                .RegisterInstance<IDataSourceProvider>("DataSource:Sex", new EnumDataSourceProvider(typeof(Sex), dataCache), new ContainerControlledLifetimeManager())
                // register database datasources
                .RegisterInstance<IDataSourceProvider>("DataSource:DataSourceCategory", new DbContextEntitySourceProvider(typeof(WebProvidersDb), typeof(DataSourceCategory), "CategoryName", "CategoryName", dataCache), new ContainerControlledLifetimeManager())
                .RegisterInstance<IDataSourceProvider>("DataSource:NavigationGroup", new DbContextEntitySourceProvider(typeof(WebProvidersDb), typeof(NavigationGroup), "Caption", "Name", dataCache), new ContainerControlledLifetimeManager())
                .RegisterInstance<IDataSourceProvider>("DataSource:NavigationItem", new DbContextEntitySourceProvider(typeof(WebProvidersDb), typeof(NavigationItem), "Caption", "Name", dataCache), new ContainerControlledLifetimeManager())
                // register data providers
                .RegisterType<IDataProvider<DataSourceCategory>, SqlDataSourceCategoryDataProvider>()
                .RegisterType<IDataProvider<DataSourceItem>, SqlDataSourceDataProvider>()
                .RegisterType<IDataProvider<FriendlyUrl>, SqlFriendlyUrlDataProvider>()
                .RegisterType<IDataProvider<LocalizationItem>, SqlLocalizationItemProvider>()
                .RegisterType<IDataProvider<Log>, SqlLogDataProvider>()
                .RegisterType<IDataProvider<NavigationGroup>, SqlNavigationGroupDataProvider>()
                .RegisterType<IDataProvider<NavigationItem>, SqlNavigationItemDataProvider>()
                .RegisterType<IDataProvider<User>, SqlUserDataProvider>()
                .RegisterType<IDataProvider<Role>, SqlRoleDataProvider>()
                .RegisterType<IDataProvider<WebPage>, SqlWebPageDataProvider>()
                .RegisterType<IDataProvider<CategoryItem>, SqlCategoryItemDataProvider>()
                .RegisterType<IDataProvider<CategoryProperty>, SqlCategoryPropertyDataProvider>();

            this.Theme = "Admin";
            this.Skin = "Default";
        }
    }
}
